# coding=utf-8

"""
@author  zhongy
@date    2018-10-17
@desc    数据集view
"""

from views.view_base import ViewBase
from controller.data_set_controller import DataSetController
from common.middlewares import *


class DataSetView(ViewBase):
    def __init__(self):
        super(DataSetView, self).__init__()

    # 添加数据集
    @dispatch('/add', methods=['POST'])
    @authenticated()
    @verify_params(data_set_name='string')
    def add(self):
        code = RESPONSE_FAILED
        msg = '添加失败!'

        try:
            data_set_name = self.params.get('data_set_name')
            controller = DataSetController()
            data_set = controller.get_by_name(data_set_name=data_set_name)
            if data_set is not None:
                code = RESPONSE_FAILED
                msg = data_set_name + '已存在!'
            else:
                result = controller.add(data_set_name=data_set_name)
                if result:
                    code = RESPONSE_SUCCESS
                    msg = '添加成功!'
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data={}, msg=msg)

    # 查询数据集列表
    @dispatch('/list', methods=['POST'])
    @authenticated()
    def list(self):
        code = RESPONSE_SUCCESS
        msg = '查询成功!'
        data = []

        try:
            data_set_name = self.params.get('data_set_name', '')
            page_index = self.params.get('page_index', 1)
            page_size = self.params.get('page_size', 5)
            controller = DataSetController()
            result, pagination = controller.list(data_set_name=data_set_name, page_index=page_index,
                                                 page_size=page_size)
            data = {'list': result, 'pagination': pagination}
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data=data, msg=msg)

    # 查询全部数据集
    @dispatch('/all', methods=['GET', 'POST'])
    @authenticated()
    def all(self):
        code = RESPONSE_SUCCESS
        msg = '查询成功!'
        data = []
        try:
            data_set_name = self.params.get('data_set_name', '')
            controller = DataSetController()
            result = controller.all(data_set_name=data_set_name)
            if result is not None:
                data = result
        except:
            writelog(traceback.format_exc())
        return self._render.render_json(code=code, data=data, msg=msg)

    # 删除数据集
    @dispatch('/delete', methods=['POST'])
    @authenticated()
    @verify_params(data_set_id='int')
    def delete(self):
        code = RESPONSE_FAILED
        msg = '删除失败!'

        try:
            data_set_id = self.params.get('data_set_id')
            controller = DataSetController()
            result = controller.update(data_set_id=data_set_id, is_deleted=True)
            if result:
                code = RESPONSE_SUCCESS
                msg = '删除成功!'
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data={}, msg=msg)